'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
Dim keywords
main
set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
		
	'//TODO
	if args ="" then args = inputbox("net type=? mipi/iic/op/usb",args,"usb")
	select case args
		case "mipi": keywords = Array("_MIPI_")
		case "iic" : keywords = Array("I2C", "SCL", "SDA", "IIC")
		case "op" : keywords = Array("TF_SD","WF_SD","_26M_","_MCLK","_26M","APC_APT","GPS_LNA_EN")
		case "usb" : keywords = Array("_D+", "_D-", "_DP", "_DM", "_DN")
	end select
	
	dim ostr:ostr=""
	doc.unselectall()
	dim pcbnet,pcbnets,netname
	set pcbnets = doc.nets(0)
	for each pcbnet in pcbnets
		netname = pcbnet.name
		if IsStrInArr(keywords,netname)=true then
			if ostr<>"" then ostr = ostr & vbcrlf
			ostr = ostr & netname
			pcbnet.selected =true
		end if	
	next	
	
	dim outfile,fso,fw
	outfile = doc.path & "output\tmp.txt"
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fw = fso.CreateTextFile(outfile, True)
	fw.write(ostr)
	fw.close
	set fw = nothing
	set fso =nothing
	
	CreateObject("WScript.Shell").Run outfile	
end sub

function IsStrInArr(keywords,targetStr)
	dim keyword, foundFlag,compareMode
	
	foundFlag = False

	' 设置比较模式：1为不区分大小写，0为区分大小写（默认）
	compareMode = 1

	For Each keyword In keywords
		If InStr(1, targetStr, keyword, compareMode) > 0 Then
			foundFlag = True
			Exit For ' 找到后立即跳出循环
		End If
	Next
	IsStrInArr = foundFlag
end function
